#include <bits/stdc++.h>
// 2025/02/04
// tag: 
// Author: Zhang Muen
using namespace std;

using int64 = int64_t;

struct {
    int iq, eq;
} cow[401];

int n, dp[800001];

signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> cow[i].iq >> cow[i].eq;
    memset(dp, -0x3f, sizeof(dp));
    dp[400000] = 0;
    for (int i = 1; i <= n; i++)
        if (cow[i].iq >= 0)
            for (int j = 800000; j >= cow[i].iq; j--)
                dp[j] = max(dp[j], dp[j - cow[i].iq] + cow[i].eq);
        else
            for (int j = 0; j <= 800000 + cow[i].iq; j++)
                dp[j] = max(dp[j], dp[j - cow[i].iq] + cow[i].eq);
    int ans = 0;
    for (int i = 400000; i <= 800000; i++)
        if (dp[i] > 0)
            ans = max(ans, dp[i] + i - 400000);
    cout << ans << endl;
    return 0;
}